/**   
 * @Title: OctoPlayController.java 
 * @Package com.android.jtf.media.hls 
 * @Description: TODO
 * @author jtf@163.com   
 * @date 2013-7-19 下午4:57:29 
 * @version V1.0   
 */
package android.framework.media.octo;

import android.app.Activity;
import android.content.Intent;
import android.framework.media.BaseMediaController;
import android.framework.media.MediaController;
import android.framework.media.PlayController;
import android.framework.media.VideoView;
import android.framework.media.bean.ShackVideoInfo;
import android.framework.util.L;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import android.framework.R;
import com.octoshape.android.client.MediaPlayerConstants;

/**
 * @ClassName: OctoPlayController
 * @Description: TODO
 * @author jtf19@163.com
 * @date 2013-7-19 下午4:57:29
 * 
 */
public class OctoPlayController implements PlayController {
	private final String TAG = "OctoPlayController";

	protected Activity activity;
	protected View dialogView;
	protected int errorCount;
	protected Bundle mBundle;
	protected MediaController mediaController;
	protected TextView loading_video;
	protected boolean more;
	protected int position;
	protected View progressView;
	protected String title;
	protected String url;
	protected VideoView videoView;
	protected Stream curStream;

	protected Octo mOcto;

	public OctoPlayController(Activity activity) {
		super();
		this.activity = activity;
	}

	private OctoListener mOctoListener = new OctoListener() {

		@Override
		public void onStreamPrepared(Stream currentStream, Uri url,
				boolean playWhenCurrentPlaybackComplete) {
			// TODO 在StreamPlayer play之后得到url之后会调用次方法 就可以调用MediaPlaryr开始播放
			L.i("stream is prepared begin to media play");
			if (videoView != null)
				videoView.release(true);
			if (!playWhenCurrentPlaybackComplete) {
				curStream = currentStream;
				L.i(curStream.toString());
				if (curStream.getSelectedPlayerId() == null) {
					onOctoshapeProblem("No suitable player found. Octoshape did not return any player!");
					return;
				}
				if ("androidmediaplayer"
						.equals(curStream.getSelectedPlayerId())) {
					// videoView = new VideoView(activity);
					videoView.setMediaController(getMediaController());
				} else {
					onOctoshapeProblem("No suitable player found");
					return;
				}
				videoView.setVideoURI(url);
				progressView.setVisibility(View.GONE);
				mediaController.show();
			} else
				curStream.urlQueue.add(url);
		}

		@Override
		public void onOctoshapeProblem(final String problemString) {
			activity.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast toast = Toast.makeText(activity, problemString,
							Toast.LENGTH_LONG);
					toast.show();
				}
			});
		}

		@Override
		public void onOctoshapePrepared() {
			// TODO octo port 拿到后会调用此方法 就可以播放流
			curStream = mOcto.initStream(url);
			curStream.requestPlayback();
		}

		@Override
		public void onOSASeekCompleted(Stream stream, Uri url, long seekOffset) {
			// TODO Auto-generated method stub
			// if (stream.isOndemand())
			// pc.setPlaySeekBarProgress((int) seekOffset);
			// else
			// pc.setPlaySeekBarProgress(pc.getPlaySeekBarMax()
			// - (int) seekOffset);
			if (videoView != null) {
				videoView.stopPlayback();
				videoView.setVideoURI(url);
			} else
				onStreamPrepared(curStream, url, false);
		}

	};

	private MediaPlayer.OnPreparedListener mOnPrePreparedListener = new OnPreparedListener() {

		@Override
		public void onPrepared(MediaPlayer mp) {
			if (curStream.isLive()) {
				if (curStream.getLiveMaxLatency() > 0) {

				} else {

				}

			} else if (curStream.isOndemand()) {

				new Thread(new Runnable() {
					public void run() {
						while (!activity.isFinishing()
								&& curStream.isOndemand()) {
							videoView.setDuration((int) curStream
									.getFileDuration());
						}
					}
				}).start();
				if (curStream.supportsNativeSeek()
						|| curStream.supportsOSASeek())
					;
			}
			curStream.setAsSeeking(false);
		}
	};

	private Handler mHandler = new Handler();

	@Override
	public String getFrom() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BaseMediaController getMediaController() {
		// TODO Auto-generated method stub
		return this.mediaController;
	}

	@Override
	public Bundle getPlayBundle() {
		// TODO Auto-generated method stub
		return mBundle;
	}

	@Override
	public ShackVideoInfo getVideoInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getVideoTitle() {
		// TODO Auto-generated method stub
		return title;
	}

	@Override
	public VideoView getVideoView() {
		// TODO Auto-generated method stub
		return videoView;
	}

	@Override
	public void handlerAdClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isLive() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isLoadingShown() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isPlayingAd() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onActivityResult(int paramInt1, int paramInt2,
			Intent paramIntent) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCreate(Bundle bundle) {
		// TODO Auto-generated method stub
		initView();
		createMediaController();
		url = bundle.getString("url");
		if (url == null || url.trim().equals("")) {
			activity.finish();
			return;
		}
		title = bundle.getString("title");
		initOcto();
	}

	private void initView() {
		this.progressView = this.activity.findViewById(R.id.play_progressview);
		RelativeLayout videoview_container = (RelativeLayout) this.activity
				.findViewById(R.id.videoview_container);
		this.videoView = new VideoView(this.activity);
		this.videoView.setOnPreparedListener(mOnPrePreparedListener);
		this.videoView.setLayoutParams(new RelativeLayout.LayoutParams(-1, -1));
		((RelativeLayout.LayoutParams) this.videoView.getLayoutParams())
				.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		videoview_container.addView(this.videoView);
		this.dialogView = this.activity.findViewById(R.id.play_loading_dialog);
		this.loading_video = ((TextView) this.activity
				.findViewById(R.id.loading_video));

	}

	protected void initOcto() {
		mOcto = new Octo(mOctoListener, activity);
		mOcto.addPlaybackInstance(MediaPlayerConstants.NATIVE_PLAYER,
				MediaPlayerConstants.NATIVE_PLAYER, "" + Build.VERSION.SDK_INT);
		mOcto.open();
		// curStream = mOcto.initStream(url);
		// curStream.requestPlayback();
	}

	/**
	 * 
	 * @Title: createMediaController
	 * @Description: 创建互动窗口
	 * @return void
	 * @throws
	 */
	protected void createMediaController() {
		this.mediaController = new MediaController(activity);
		this.mediaController.setPlayController(this);
		this.mediaController.setWebPlay(false);
		this.mediaController
				.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		if (videoView != null) {
			videoView.stopPlayback();
		}
		if (mOcto != null)
			mOcto.finish();
	}

	@Override
	public void onFinish() {
		// TODO Auto-generated method stub
		if (this.videoView != null)
			this.videoView.stopPlayback();
		if (mOcto != null)
			mOcto.finish();

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onVideoPause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onVideoStart() {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateVideoPosition() {
		// TODO Auto-generated method stub

	}

	private OnSeekBarChangeListener mOnSeekBarChangeListener = new OnSeekBarChangeListener() {

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {

			// TODO Auto-generated method stub
			L.e("onStopTrackingTouch  2  " + seekBar.getProgress());
			if (curStream.isOndemand()) {

				if (seekBar.getProgress() == seekBar.getMax()) {
					videoView.stopPlayback();
					return;
				}
				if (curStream.supportsNativeSeek()) {
					L.i("supportsNativeSeek");
					videoView.seekTo((int) (videoView.getDuration()
							* seekBar.getProgress() / 1000L));

				} else {
					L.i("can not supportsNativeSeek");
					int osaSeekPosition = (int) (videoView.getDuration()
							* seekBar.getProgress() / 1000L);
					L.i(osaSeekPosition + "");
					curStream.requestOsaSeek(osaSeekPosition);
				}
			} else {
				L.i("isLive");
				curStream.requestDVR((int) ((seekBar.getMax() - seekBar
						.getProgress()) * videoView.getDuration() / 1000L));

			}

		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {

		}
	};

	public boolean onTouchEvent(MotionEvent paramMotionEvent) {
		this.mediaController.onTouchEvent(paramMotionEvent);
		return false;
	}
}
